package com.dooioo.fangyou.web;

import com.dooioo.fangyou.entity.Pet;
import com.dooioo.fangyou.entity.PetType;
import com.dooioo.fangyou.service.PetService;
import com.dooioo.fangyou.service.PetTypeService;
import com.dooioo.fangyou.web.validation.PetValidator;
import com.sun.net.httpserver.HttpsServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.HttpServletBean;

import java.util.Collection;
import java.util.List;

/**
 * com.dooioo.fangyou.web
 * User: kqy
 * Date: 2010-12-2
 * Time: 14:50:29
 */

@Controller
@RequestMapping(value = "/pet")
@SessionAttributes("pet")
public class PetController {

    @Autowired
    private PetService petService;
    @Autowired
    private PetTypeService petTypeService;
    
    @InitBinder
	public void setAllowedFields(WebDataBinder dataBinder) {
		dataBinder.setDisallowedFields("id");
	}

    @RequestMapping(method =RequestMethod.GET, value = "/list")
    public String list(Model model) {
        List<Pet> petList = petService.queryForList();
        model.addAttribute("petList", petList);
        return "/pet/list";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public String view(@PathVariable("id") String id,  Model model){
        Pet pet = (Pet)petService.findBy("id", id);
        model.addAttribute(pet);
        return "/pet/view";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/{id}/edit")
    public String setupForm(@PathVariable("id") String id,  Model model) {
        Pet pet = (Pet)petService.findBy("id", id);
        List<PetType> types =  petTypeService.queryForList();
		model.addAttribute("pet", pet);
		model.addAttribute("types", types);
        return "/pet/edit";
    }

    @RequestMapping(method = { RequestMethod.PUT }, value = "/{id}/edit")
    public String processUpdate(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
        petService.updatePet(pet);
        status.setComplete();
        return "/pet/view";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/new")
    public String setupAddForm(Model model) {
        Pet pet = new Pet();
        List<PetType> types =  petTypeService.queryForList();
		model.addAttribute("pet", pet);
		model.addAttribute("types", types);
        return "/pet/edit";
    }

    @RequestMapping(method = RequestMethod.POST)
	public String processAddSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
		new PetValidator().validate(pet, result);
		if (result.hasErrors()) {
			return "/pet/edit";
		}
		else {
            petService.storePet(pet);
			status.setComplete();
			return "redirect:/";
		}
	}

    @RequestMapping(method = RequestMethod.DELETE)
	public String deletePet(@PathVariable String id) {
        petService.delete(id);
		return "redirect:/pet/list";
	}
}
